import React from 'react';
import {Button, Form, Input, message, Select, Card, Row, Col} from "antd";
import {initSqlDoc} from "@/config";
import {download} from "@/plugins/axios";

interface SqlDocFormValues {
    host: string;
    port: number;
    userName: string;
    password: string;
    dbName: string;
    description: string;
    version: string;
    fileOutputDir: string;
    fileType: 'MD' | 'HTML' | 'WORD';
    ignorePrefix: string[];
    ignoreSuffix: string[];
    ignoreTableName: string[];
}

const SqlDoc: React.FC = () => {
    const [form] = Form.useForm<SqlDocFormValues>();
    const [loading, setLoading] = React.useState<boolean>(false);

    const onFinish = async (values: SqlDocFormValues) => {
        try {
            setLoading(true);
            await download(`/code/generateSQLDocument`,values);
            message.success('文档生成成功');
        } catch (error) {
            console.error('生成文档失败:', error);
            message.error('生成文档失败');
        } finally {
            setLoading(false);
        }
    };

    return (
        <div style={{padding: '24px'}}>
            <Card bordered={false}>
                <Form
                    form={form}
                    initialValues={initSqlDoc}
                    onFinish={onFinish}
                    layout="vertical"
                    size="middle"
                >
                    <Row gutter={16}>
                        <Col span={8}>
                            <Form.Item
                                name="host"
                                label="数据库地址"
                            >
                                <Input placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="port"
                                label="端口"
                            >
                                <Input placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="dbName"
                                label="数据库名"
                            >
                                <Input placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                    </Row>

                    <Row gutter={16}>
                        <Col span={8}>
                            <Form.Item
                                name="userName"
                                label="用户名"
                            >
                                <Input placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="password"
                                label="密码"
                            >
                                <Input.Password placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                    </Row>

                    <Row gutter={16}>
                        <Col span={8}>
                            <Form.Item
                                name="version"
                                label="版本"
                            >
                                <Input placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="description"
                                label="描述"
                            >
                                <Input placeholder="请输入" allowClear/>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="fileType"
                                label="文件类型"
                            >
                                <Select options={[
                                    {value: 'MD', label: 'Markdown文件'},
                                    {value: 'HTML', label: 'HTML文件'},
                                    {value: 'WORD', label: 'WORD文件'}]}/>
                            </Form.Item>
                        </Col>
                    </Row>

                    <Row gutter={16}>
                        <Col span={8}>
                            <Form.Item
                                name="designatedTableName"
                                label="指定表"
                            >
                                <Select
                                    mode="tags"
                                    placeholder="请输入，回车确认"
                                    allowClear
                                    style={{width: '100%'}}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="designatedTablePrefix"
                                label="指定前缀"
                            >
                                <Select
                                    mode="tags"
                                    placeholder="请输入，回车确认"
                                    allowClear
                                    style={{width: '100%'}}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="designatedTableSuffix"
                                label="指定后缀"
                            >
                                <Select
                                    mode="tags"
                                    placeholder="请输入，回车确认"
                                    allowClear
                                    style={{width: '100%'}}
                                />
                            </Form.Item>
                        </Col>
                    </Row>

                    <Row gutter={16}>
                        <Col span={8}>
                            <Form.Item
                                name="ignoreTableName"
                                label="忽略表名"
                            >
                                <Select
                                    mode="tags"
                                    placeholder="请输入表名，回车确认"
                                    allowClear
                                    style={{width: '100%'}}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="ignorePrefix"
                                label="忽略前缀"
                            >
                                <Select
                                    mode="tags"
                                    placeholder="请输入前缀，回车确认"
                                    allowClear
                                    style={{width: '100%'}}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                name="ignoreSuffix"
                                label="忽略后缀"
                            >
                                <Select
                                    mode="tags"
                                    placeholder="请输入后缀，回车确认"
                                    allowClear
                                    style={{width: '100%'}}
                                />
                            </Form.Item>
                        </Col>
                    </Row>

                    <Form.Item>
                        <Button 
                            type="primary" 
                            block 
                            htmlType="submit"
                            loading={loading}
                        >
                            {loading ? '生成中...' : '生成文档'}
                        </Button>
                    </Form.Item>
                </Form>
            </Card>
        </div>
    );
};

export default SqlDoc;